<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.utils.html &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-html">
            
  <h1>Source code for django.utils.html</h1><div class="highlight"><pre>
<span class="sd">&quot;&quot;&quot;HTML utilities suitable for global use.&quot;&quot;&quot;</span>

<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInDjango18Warning</span>
<span class="kn">from</span> <span class="nn">django.utils.encoding</span> <span class="kn">import</span> <span class="n">force_text</span><span class="p">,</span> <span class="n">force_str</span>
<span class="kn">from</span> <span class="nn">django.utils.functional</span> <span class="kn">import</span> <span class="n">allow_lazy</span>
<span class="kn">from</span> <span class="nn">django.utils.safestring</span> <span class="kn">import</span> <span class="n">SafeData</span><span class="p">,</span> <span class="n">mark_safe</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.six.moves.urllib.parse</span> <span class="kn">import</span> <span class="n">quote</span><span class="p">,</span> <span class="n">unquote</span><span class="p">,</span> <span class="n">urlsplit</span><span class="p">,</span> <span class="n">urlunsplit</span>
<span class="kn">from</span> <span class="nn">django.utils.text</span> <span class="kn">import</span> <span class="n">normalize_newlines</span>

<span class="kn">from</span> <span class="nn">.html_parser</span> <span class="kn">import</span> <span class="n">HTMLParser</span><span class="p">,</span> <span class="n">HTMLParseError</span>


<span class="c"># Configuration for urlize() function.</span>
<span class="n">TRAILING_PUNCTUATION</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;.&#39;</span><span class="p">,</span> <span class="s">&#39;,&#39;</span><span class="p">,</span> <span class="s">&#39;:&#39;</span><span class="p">,</span> <span class="s">&#39;;&#39;</span><span class="p">,</span> <span class="s">&#39;.)&#39;</span><span class="p">,</span> <span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\&#39;</span><span class="s">&#39;</span><span class="p">]</span>
<span class="n">WRAPPING_PUNCTUATION</span> <span class="o">=</span> <span class="p">[(</span><span class="s">&#39;(&#39;</span><span class="p">,</span> <span class="s">&#39;)&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;&lt;&#39;</span><span class="p">,</span> <span class="s">&#39;&gt;&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;[&#39;</span><span class="p">,</span> <span class="s">&#39;]&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;&amp;lt;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;gt;&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&#39;&quot;&#39;</span><span class="p">),</span> <span class="p">(</span><span class="s">&#39;</span><span class="se">\&#39;</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\&#39;</span><span class="s">&#39;</span><span class="p">)]</span>

<span class="c"># List of possible strings used for bullets in bulleted lists.</span>
<span class="n">DOTS</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;&amp;middot;&#39;</span><span class="p">,</span> <span class="s">&#39;*&#39;</span><span class="p">,</span> <span class="s">&#39;</span><span class="se">\u2022</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;#149;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;bull;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;#8226;&#39;</span><span class="p">]</span>

<span class="n">unencoded_ampersands_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;&amp;(?!(\w+|#\d+);)&#39;</span><span class="p">)</span>
<span class="n">word_split_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;(\s+)&#39;</span><span class="p">)</span>
<span class="n">simple_url_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^https?://\[?\w&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
<span class="n">simple_url_2_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^www\.|^(?!http)\w[^@]+\.(com|edu|gov|int|mil|net|org)$&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
<span class="n">simple_email_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;^\S+@\S+\.\S+$&#39;</span><span class="p">)</span>
<span class="n">link_target_attribute_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;(&lt;a [^&gt;]*?)target=[^\s&gt;]+&#39;</span><span class="p">)</span>
<span class="n">html_gunk_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;(?:&lt;br clear=&quot;all&quot;&gt;|&lt;i&gt;&lt;\/i&gt;|&lt;b&gt;&lt;\/b&gt;|&lt;em&gt;&lt;\/em&gt;|&lt;strong&gt;&lt;\/strong&gt;|&lt;\/?smallcaps&gt;|&lt;\/?uppercase&gt;)&#39;</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">IGNORECASE</span><span class="p">)</span>
<span class="n">hard_coded_bullets_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;((?:&lt;p&gt;(?:</span><span class="si">%s</span><span class="s">).*?[a-zA-Z].*?&lt;/p&gt;\s*)+)&#39;</span> <span class="o">%</span> <span class="s">&#39;|&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">DOTS</span><span class="p">),</span> <span class="n">re</span><span class="o">.</span><span class="n">DOTALL</span><span class="p">)</span>
<span class="n">trailing_empty_content_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;(?:&lt;p&gt;(?:&amp;nbsp;|\s|&lt;br \/&gt;)*?&lt;/p&gt;\s*)+\Z&#39;</span><span class="p">)</span>


<div class="viewcode-block" id="escape"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.escape">[docs]</a><span class="k">def</span> <span class="nf">escape</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns the given text with ampersands, quotes and angle brackets encoded</span>
<span class="sd">    for use in HTML.</span>

<span class="sd">    This function always escapes its input, even if it&#39;s already escaped and</span>
<span class="sd">    marked as such. This may result in double-escaping. If this is a concern,</span>
<span class="sd">    use conditional_escape() instead.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">text</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&amp;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;amp;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&lt;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;lt;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&gt;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;gt;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">,</span> <span class="s">&#39;&amp;quot;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&quot;&#39;&quot;</span><span class="p">,</span> <span class="s">&#39;&amp;#39;&#39;</span><span class="p">))</span></div>
<span class="n">escape</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">escape</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>

<span class="n">_js_escapes</span> <span class="o">=</span> <span class="p">{</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\</span><span class="s">&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u005C&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\&#39;</span><span class="s">&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u0027&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;&quot;&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u0022&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;&gt;&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u003E&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;&lt;&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u003C&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;&amp;&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u0026&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;=&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u003D&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;-&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u002D&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;;&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u003B&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\u2028</span><span class="s">&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u2028&#39;</span><span class="p">,</span>
    <span class="nb">ord</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\u2029</span><span class="s">&#39;</span><span class="p">):</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u2029&#39;</span>
<span class="p">}</span>

<span class="c"># Escape every ASCII character with a value less than 32.</span>
<span class="n">_js_escapes</span><span class="o">.</span><span class="n">update</span><span class="p">((</span><span class="nb">ord</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%c</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">z</span><span class="p">),</span> <span class="s">&#39;</span><span class="se">\\</span><span class="s">u</span><span class="si">%04X</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">z</span><span class="p">)</span> <span class="k">for</span> <span class="n">z</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">32</span><span class="p">))</span>


<span class="k">def</span> <span class="nf">escapejs</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Hex encodes characters for use in JavaScript strings.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="n">_js_escapes</span><span class="p">))</span>
<span class="n">escapejs</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">escapejs</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<div class="viewcode-block" id="conditional_escape"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.conditional_escape">[docs]</a><span class="k">def</span> <span class="nf">conditional_escape</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Similar to escape(), except that it doesn&#39;t operate on pre-escaped strings.</span>

<span class="sd">    This function relies on the __html__ convention used both by Django&#39;s</span>
<span class="sd">    SafeData class and by third-party libraries like markupsafe.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="s">&#39;__html__&#39;</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">text</span><span class="o">.</span><span class="n">__html__</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">escape</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="format_html"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.format_html">[docs]</a><span class="k">def</span> <span class="nf">format_html</span><span class="p">(</span><span class="n">format_string</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Similar to str.format, but passes all arguments through conditional_escape,</span>
<span class="sd">    and calls &#39;mark_safe&#39; on the result. This function should be used instead</span>
<span class="sd">    of str.format or % interpolation to build up small HTML fragments.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">args_safe</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="n">conditional_escape</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span>
    <span class="n">kwargs_safe</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">k</span><span class="p">,</span> <span class="n">conditional_escape</span><span class="p">(</span><span class="n">v</span><span class="p">))</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">six</span><span class="o">.</span><span class="n">iteritems</span><span class="p">(</span><span class="n">kwargs</span><span class="p">))</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">format_string</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="o">*</span><span class="n">args_safe</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs_safe</span><span class="p">))</span>

</div>
<div class="viewcode-block" id="format_html_join"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.format_html_join">[docs]</a><span class="k">def</span> <span class="nf">format_html_join</span><span class="p">(</span><span class="n">sep</span><span class="p">,</span> <span class="n">format_string</span><span class="p">,</span> <span class="n">args_generator</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    A wrapper of format_html, for the common case of a group of arguments that</span>
<span class="sd">    need to be formatted using the same format string, and then joined using</span>
<span class="sd">    &#39;sep&#39;. &#39;sep&#39; is also passed through conditional_escape.</span>

<span class="sd">    &#39;args_generator&#39; should be an iterator that returns the sequence of &#39;args&#39;</span>
<span class="sd">    that will be passed to format_html.</span>

<span class="sd">    Example:</span>

<span class="sd">      format_html_join(&#39;\n&#39;, &quot;&lt;li&gt;{0} {1}&lt;/li&gt;&quot;, ((u.first_name, u.last_name)</span>
<span class="sd">                                                  for u in users))</span>

<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">conditional_escape</span><span class="p">(</span><span class="n">sep</span><span class="p">)</span><span class="o">.</span><span class="n">join</span><span class="p">(</span>
        <span class="n">format_html</span><span class="p">(</span><span class="n">format_string</span><span class="p">,</span> <span class="o">*</span><span class="nb">tuple</span><span class="p">(</span><span class="n">args</span><span class="p">))</span>
        <span class="k">for</span> <span class="n">args</span> <span class="ow">in</span> <span class="n">args_generator</span><span class="p">))</span>

</div>
<span class="k">def</span> <span class="nf">linebreaks</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">autoescape</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Converts newlines into &lt;p&gt; and &lt;br /&gt;s.&quot;&quot;&quot;</span>
    <span class="n">value</span> <span class="o">=</span> <span class="n">normalize_newlines</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="n">paras</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">{2,}&#39;</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">autoescape</span><span class="p">:</span>
        <span class="n">paras</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;&lt;p&gt;</span><span class="si">%s</span><span class="s">&lt;/p&gt;&#39;</span> <span class="o">%</span> <span class="n">escape</span><span class="p">(</span><span class="n">p</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;br /&gt;&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">paras</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">paras</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;&lt;p&gt;</span><span class="si">%s</span><span class="s">&lt;/p&gt;&#39;</span> <span class="o">%</span> <span class="n">p</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;br /&gt;&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">paras</span><span class="p">]</span>
    <span class="k">return</span> <span class="s">&#39;</span><span class="se">\n\n</span><span class="s">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">paras</span><span class="p">)</span>
<span class="n">linebreaks</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">linebreaks</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">MLStripper</span><span class="p">(</span><span class="n">HTMLParser</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">six</span><span class="o">.</span><span class="n">PY2</span><span class="p">:</span>
            <span class="n">HTMLParser</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">HTMLParser</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">handle_data</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">d</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">d</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle_entityref</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;&amp;</span><span class="si">%s</span><span class="s">;&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">handle_charref</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;&amp;#</span><span class="si">%s</span><span class="s">;&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_data</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fed</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">_strip_once</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Internal tag stripping utility used by strip_tags.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">s</span> <span class="o">=</span> <span class="n">MLStripper</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">s</span><span class="o">.</span><span class="n">feed</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
    <span class="k">except</span> <span class="n">HTMLParseError</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">value</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">s</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
    <span class="k">except</span> <span class="p">(</span><span class="n">HTMLParseError</span><span class="p">,</span> <span class="ne">UnboundLocalError</span><span class="p">):</span>
        <span class="c"># UnboundLocalError because of http://bugs.python.org/issue17802</span>
        <span class="c"># on Python 3.2, triggered by strict=False mode of HTMLParser</span>
        <span class="k">return</span> <span class="n">s</span><span class="o">.</span><span class="n">get_data</span><span class="p">()</span> <span class="o">+</span> <span class="n">s</span><span class="o">.</span><span class="n">rawdata</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">s</span><span class="o">.</span><span class="n">get_data</span><span class="p">()</span>


<div class="viewcode-block" id="strip_tags"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.strip_tags">[docs]</a><span class="k">def</span> <span class="nf">strip_tags</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns the given HTML with all tags stripped.&quot;&quot;&quot;</span>
    <span class="c"># Note: in typical case this loop executes _strip_once once. Loop condition</span>
    <span class="c"># is redundant, but helps to reduce number of executions of _strip_once.</span>
    <span class="k">while</span> <span class="s">&#39;&lt;&#39;</span> <span class="ow">in</span> <span class="n">value</span> <span class="ow">and</span> <span class="s">&#39;&gt;&#39;</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
        <span class="n">new_value</span> <span class="o">=</span> <span class="n">_strip_once</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">new_value</span><span class="p">)</span> <span class="o">&gt;=</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
            <span class="c"># _strip_once was not able to detect more tags or length increased</span>
            <span class="c"># due to http://bugs.python.org/issue20288</span>
            <span class="c"># (affects Python 2 &lt; 2.7.7 and Python 3 &lt; 3.3.5)</span>
            <span class="k">break</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">new_value</span>
    <span class="k">return</span> <span class="n">value</span></div>
<span class="n">strip_tags</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">strip_tags</span><span class="p">)</span>


<div class="viewcode-block" id="remove_tags"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.html.remove_tags">[docs]</a><span class="k">def</span> <span class="nf">remove_tags</span><span class="p">(</span><span class="n">html</span><span class="p">,</span> <span class="n">tags</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns the given HTML with given tags removed.&quot;&quot;&quot;</span>
    <span class="n">tags</span> <span class="o">=</span> <span class="p">[</span><span class="n">re</span><span class="o">.</span><span class="n">escape</span><span class="p">(</span><span class="n">tag</span><span class="p">)</span> <span class="k">for</span> <span class="n">tag</span> <span class="ow">in</span> <span class="n">tags</span><span class="o">.</span><span class="n">split</span><span class="p">()]</span>
    <span class="n">tags_re</span> <span class="o">=</span> <span class="s">&#39;(</span><span class="si">%s</span><span class="s">)&#39;</span> <span class="o">%</span> <span class="s">&#39;|&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">tags</span><span class="p">)</span>
    <span class="n">starttag_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">r&#39;&lt;</span><span class="si">%s</span><span class="s">(/?&gt;|(\s+[^&gt;]*&gt;))&#39;</span> <span class="o">%</span> <span class="n">tags_re</span><span class="p">,</span> <span class="n">re</span><span class="o">.</span><span class="n">U</span><span class="p">)</span>
    <span class="n">endtag_re</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="s">&#39;&lt;/</span><span class="si">%s</span><span class="s">&gt;&#39;</span> <span class="o">%</span> <span class="n">tags_re</span><span class="p">)</span>
    <span class="n">html</span> <span class="o">=</span> <span class="n">starttag_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span>
    <span class="n">html</span> <span class="o">=</span> <span class="n">endtag_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">html</span></div>
<span class="n">remove_tags</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">remove_tags</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">strip_spaces_between_tags</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns the given HTML with spaces between tags removed.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&#39;&gt;\s+&lt;&#39;</span><span class="p">,</span> <span class="s">&#39;&gt;&lt;&#39;</span><span class="p">,</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
<span class="n">strip_spaces_between_tags</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">strip_spaces_between_tags</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">strip_entities</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns the given HTML with all entities (&amp;something;) stripped.&quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&#39;&amp;(?:\w+|#\d+);&#39;</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
<span class="n">strip_entities</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">strip_entities</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">fix_ampersands</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Returns the given HTML with all unencoded ampersands encoded correctly.&quot;&quot;&quot;</span>
    <span class="c"># As fix_ampersands is wrapped in allow_lazy, stacklevel 3 is more useful than 2.</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;The fix_ampersands function is deprecated and will be removed in Django 1.8.&quot;</span><span class="p">,</span>
                  <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">unencoded_ampersands_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;&amp;amp;&#39;</span><span class="p">,</span> <span class="n">force_text</span><span class="p">(</span><span class="n">value</span><span class="p">))</span>
<span class="n">fix_ampersands</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">fix_ampersands</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">smart_urlquote</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
    <span class="s">&quot;Quotes a URL if it isn&#39;t already quoted.&quot;</span>
    <span class="c"># Handle IDN before quoting.</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span> <span class="o">=</span> <span class="n">urlsplit</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">netloc</span> <span class="o">=</span> <span class="n">netloc</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;ascii&#39;</span><span class="p">)</span>  <span class="c"># IDN -&gt; ACE</span>
        <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>  <span class="c"># invalid domain part</span>
            <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">url</span> <span class="o">=</span> <span class="n">urlunsplit</span><span class="p">((</span><span class="n">scheme</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">query</span><span class="p">,</span> <span class="n">fragment</span><span class="p">))</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="c"># invalid IPv6 URL (normally square brackets in hostname part).</span>
        <span class="k">pass</span>

    <span class="n">url</span> <span class="o">=</span> <span class="n">unquote</span><span class="p">(</span><span class="n">force_str</span><span class="p">(</span><span class="n">url</span><span class="p">))</span>
    <span class="c"># See http://bugs.python.org/issue2637</span>
    <span class="n">url</span> <span class="o">=</span> <span class="n">quote</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">safe</span><span class="o">=</span><span class="n">b</span><span class="s">&#39;!*</span><span class="se">\&#39;</span><span class="s">();:@&amp;=+$,/?#[]~&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">force_text</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">urlize</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">trim_url_limit</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">nofollow</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">autoescape</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Converts any URLs in text into clickable links.</span>

<span class="sd">    Works on http://, https://, www. links, and also on links ending in one of</span>
<span class="sd">    the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org).</span>
<span class="sd">    Links can have trailing punctuation (periods, commas, close-parens) and</span>
<span class="sd">    leading punctuation (opening parens) and it&#39;ll still do the right thing.</span>

<span class="sd">    If trim_url_limit is not None, the URLs in the link text longer than this</span>
<span class="sd">    limit will be truncated to trim_url_limit-3 characters and appended with</span>
<span class="sd">    an ellipsis.</span>

<span class="sd">    If nofollow is True, the links will get a rel=&quot;nofollow&quot; attribute.</span>

<span class="sd">    If autoescape is True, the link text and URLs will be autoescaped.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">trim_url</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">limit</span><span class="o">=</span><span class="n">trim_url_limit</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">limit</span> <span class="ow">is</span> <span class="bp">None</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">limit</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">x</span>
        <span class="k">return</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">...&#39;</span> <span class="o">%</span> <span class="n">x</span><span class="p">[:</span><span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">limit</span> <span class="o">-</span> <span class="mi">3</span><span class="p">)]</span>
    <span class="n">safe_input</span> <span class="o">=</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">SafeData</span><span class="p">)</span>
    <span class="n">words</span> <span class="o">=</span> <span class="n">word_split_re</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="n">force_text</span><span class="p">(</span><span class="n">text</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">word</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">words</span><span class="p">):</span>
        <span class="k">if</span> <span class="s">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">word</span> <span class="ow">or</span> <span class="s">&#39;@&#39;</span> <span class="ow">in</span> <span class="n">word</span> <span class="ow">or</span> <span class="s">&#39;:&#39;</span> <span class="ow">in</span> <span class="n">word</span><span class="p">:</span>
            <span class="c"># Deal with punctuation.</span>
            <span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span> <span class="o">=</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">word</span><span class="p">,</span> <span class="s">&#39;&#39;</span>
            <span class="k">for</span> <span class="n">punctuation</span> <span class="ow">in</span> <span class="n">TRAILING_PUNCTUATION</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">middle</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">punctuation</span><span class="p">):</span>
                    <span class="n">middle</span> <span class="o">=</span> <span class="n">middle</span><span class="p">[:</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">punctuation</span><span class="p">)]</span>
                    <span class="n">trail</span> <span class="o">=</span> <span class="n">punctuation</span> <span class="o">+</span> <span class="n">trail</span>
            <span class="k">for</span> <span class="n">opening</span><span class="p">,</span> <span class="n">closing</span> <span class="ow">in</span> <span class="n">WRAPPING_PUNCTUATION</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">middle</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">opening</span><span class="p">):</span>
                    <span class="n">middle</span> <span class="o">=</span> <span class="n">middle</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">opening</span><span class="p">):]</span>
                    <span class="n">lead</span> <span class="o">=</span> <span class="n">lead</span> <span class="o">+</span> <span class="n">opening</span>
                <span class="c"># Keep parentheses at the end only if they&#39;re balanced.</span>
                <span class="k">if</span> <span class="p">(</span><span class="n">middle</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="n">closing</span><span class="p">)</span>
                        <span class="ow">and</span> <span class="n">middle</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">closing</span><span class="p">)</span> <span class="o">==</span> <span class="n">middle</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="n">opening</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
                    <span class="n">middle</span> <span class="o">=</span> <span class="n">middle</span><span class="p">[:</span><span class="o">-</span><span class="nb">len</span><span class="p">(</span><span class="n">closing</span><span class="p">)]</span>
                    <span class="n">trail</span> <span class="o">=</span> <span class="n">closing</span> <span class="o">+</span> <span class="n">trail</span>

            <span class="c"># Make URL we want to point to.</span>
            <span class="n">url</span> <span class="o">=</span> <span class="bp">None</span>
            <span class="n">nofollow_attr</span> <span class="o">=</span> <span class="s">&#39; rel=&quot;nofollow&quot;&#39;</span> <span class="k">if</span> <span class="n">nofollow</span> <span class="k">else</span> <span class="s">&#39;&#39;</span>
            <span class="k">if</span> <span class="n">simple_url_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">middle</span><span class="p">):</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">smart_urlquote</span><span class="p">(</span><span class="n">middle</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">simple_url_2_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">middle</span><span class="p">):</span>
                <span class="n">url</span> <span class="o">=</span> <span class="n">smart_urlquote</span><span class="p">(</span><span class="s">&#39;http://</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">middle</span><span class="p">)</span>
            <span class="k">elif</span> <span class="s">&#39;:&#39;</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">middle</span> <span class="ow">and</span> <span class="n">simple_email_re</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">middle</span><span class="p">):</span>
                <span class="n">local</span><span class="p">,</span> <span class="n">domain</span> <span class="o">=</span> <span class="n">middle</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s">&#39;@&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">domain</span> <span class="o">=</span> <span class="n">domain</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="s">&#39;idna&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">decode</span><span class="p">(</span><span class="s">&#39;ascii&#39;</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">UnicodeError</span><span class="p">:</span>
                    <span class="k">continue</span>
                <span class="n">url</span> <span class="o">=</span> <span class="s">&#39;mailto:</span><span class="si">%s</span><span class="s">@</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">local</span><span class="p">,</span> <span class="n">domain</span><span class="p">)</span>
                <span class="n">nofollow_attr</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>

            <span class="c"># Make link.</span>
            <span class="k">if</span> <span class="n">url</span><span class="p">:</span>
                <span class="n">trimmed</span> <span class="o">=</span> <span class="n">trim_url</span><span class="p">(</span><span class="n">middle</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">autoescape</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">safe_input</span><span class="p">:</span>
                    <span class="n">lead</span><span class="p">,</span> <span class="n">trail</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">lead</span><span class="p">),</span> <span class="n">escape</span><span class="p">(</span><span class="n">trail</span><span class="p">)</span>
                    <span class="n">url</span><span class="p">,</span> <span class="n">trimmed</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">url</span><span class="p">),</span> <span class="n">escape</span><span class="p">(</span><span class="n">trimmed</span><span class="p">)</span>
                <span class="n">middle</span> <span class="o">=</span> <span class="s">&#39;&lt;a href=&quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">&gt;</span><span class="si">%s</span><span class="s">&lt;/a&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">nofollow_attr</span><span class="p">,</span> <span class="n">trimmed</span><span class="p">)</span>
                <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s%s%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">lead</span><span class="p">,</span> <span class="n">middle</span><span class="p">,</span> <span class="n">trail</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">safe_input</span><span class="p">:</span>
                    <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
                <span class="k">elif</span> <span class="n">autoescape</span><span class="p">:</span>
                    <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">safe_input</span><span class="p">:</span>
            <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">mark_safe</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">autoescape</span><span class="p">:</span>
            <span class="n">words</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">escape</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
    <span class="k">return</span> <span class="s">&#39;&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">words</span><span class="p">)</span>
<span class="n">urlize</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">urlize</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">clean_html</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Clean the given HTML.  Specifically, do the following:</span>
<span class="sd">        * Convert &lt;b&gt; and &lt;i&gt; to &lt;strong&gt; and &lt;em&gt;.</span>
<span class="sd">        * Encode all ampersands correctly.</span>
<span class="sd">        * Remove all &quot;target&quot; attributes from &lt;a&gt; tags.</span>
<span class="sd">        * Remove extraneous HTML, such as presentational tags that open and</span>
<span class="sd">          immediately close and &lt;br clear=&quot;all&quot;&gt;.</span>
<span class="sd">        * Convert hard-coded bullets into HTML unordered lists.</span>
<span class="sd">        * Remove stuff like &quot;&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;&quot;, but only if it&#39;s at the</span>
<span class="sd">          bottom of the text.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="c"># As clean_html is wrapped in allow_lazy, stacklevel 3 is more useful than 2.</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;The clean_html function is deprecated and will be removed in Django 1.8.&quot;</span><span class="p">,</span>
                  <span class="n">RemovedInDjango18Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">normalize_newlines</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&#39;&lt;(/?)\s*b\s*&gt;&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;</span><span class="se">\\</span><span class="s">1strong&gt;&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">r&#39;&lt;(/?)\s*i\s*&gt;&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;</span><span class="se">\\</span><span class="s">1em&gt;&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">fix_ampersands</span><span class="p">(</span><span class="n">text</span><span class="p">)</span>
    <span class="c"># Remove all target=&quot;&quot; attributes from &lt;a&gt; tags.</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">link_target_attribute_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;</span><span class="se">\\</span><span class="s">1&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="c"># Trim stupid HTML such as &lt;br clear=&quot;all&quot;&gt;.</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">html_gunk_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="c"># Convert hard-coded bullets into HTML unordered lists.</span>

    <span class="k">def</span> <span class="nf">replace_p_tags</span><span class="p">(</span><span class="n">match</span><span class="p">):</span>
        <span class="n">s</span> <span class="o">=</span> <span class="n">match</span><span class="o">.</span><span class="n">group</span><span class="p">()</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&lt;/p&gt;&#39;</span><span class="p">,</span> <span class="s">&#39;&lt;/li&gt;&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">DOTS</span><span class="p">:</span>
            <span class="n">s</span> <span class="o">=</span> <span class="n">s</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&#39;&lt;p&gt;</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="n">d</span><span class="p">,</span> <span class="s">&#39;&lt;li&gt;&#39;</span><span class="p">)</span>
        <span class="k">return</span> <span class="s">&#39;&lt;ul&gt;</span><span class="se">\n</span><span class="si">%s</span><span class="se">\n</span><span class="s">&lt;/ul&gt;&#39;</span> <span class="o">%</span> <span class="n">s</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">hard_coded_bullets_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">replace_p_tags</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="c"># Remove stuff like &quot;&lt;p&gt;&amp;nbsp;&amp;nbsp;&lt;/p&gt;&quot;, but only if it&#39;s at the bottom</span>
    <span class="c"># of the text.</span>
    <span class="n">text</span> <span class="o">=</span> <span class="n">trailing_empty_content_re</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">text</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">text</span>
<span class="n">clean_html</span> <span class="o">=</span> <span class="n">allow_lazy</span><span class="p">(</span><span class="n">clean_html</span><span class="p">,</span> <span class="n">six</span><span class="o">.</span><span class="n">text_type</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">avoid_wrapping</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Avoid text wrapping in the middle of a phrase by adding non-breaking</span>
<span class="sd">    spaces where there previously were normal spaces.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">return</span> <span class="n">value</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s">&quot; &quot;</span><span class="p">,</span> <span class="s">&quot;</span><span class="se">\xa0</span><span class="s">&quot;</span><span class="p">)</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.utils.html</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>